/*
 * SSL Enumerator (ssl-enum) is Copyright (C) 2009-2014 David Kierznowski
 * (withdk.com).
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.
 *
 */

#include <stdio.h>    
#include <sys/socket.h>
#include <arpa/inet.h>  
#include <stdlib.h>     
#include <string.h>    
#include <unistd.h>     
#include <signal.h>     
#include <errno.h>		
#include <stdint.h>		
#include <sys/types.h>
#include <time.h> //defines time() and is needed for randomize()

#define RCVBUFSIZE 5000 /* Size of receive buffer */
#define DEFAULT_TCP_CONNECT_TIMEOUT 10  /* TCP connect timeout in seconds */
#define DEFAULT_RECV_TIMEOUT 30 /* TCP recv timeout in seconds */

/*
	Record Layer Protocol Types
		20 (0x14) ChangeCipherSpec
		21 (0x15) Alert
		22 (0x16) Handshake
		23 (0x17) Application Protocol data
*/
#define SSLCHANGECIPHERSPEC 20 /* We dont currently use this */
#define SSLALERT 21
#define SSLHANDSHAKE 22
#define SSLDATA 23 /* We dont currently use this */

/* 
	SSL2 differences 
	
	128 (0x80) Handshake
*/
#define SSL2_HANDSHAKE 128
#define SSL2_SVRHANDSHAKE 83
#define SSL2_CIPHERLEN 0x0003
#define SSL2_CHALLENGE_LEN 18

/* Protocol versions */
#define SSL2_VERSION 0x0002
#define SSL3_VERSION 0x0300
#define TLS1_VERSION 0x0301

/* Hello messages */
#define CLIENT_HELLO 0x01
#define SERVER_HELLO 0x02

/* Bytes of interest */
#define SSLALERTBYTE 06
#define SERVERHELLOBYTE 05
#define SSL2_SERVERHELLOBYTE 3

/* For clientHello struct */
#define SESSIONID 0x0000
#define CIPHER_LEN 0x0002
#define COMPRESSION_LEN 0x01
#define COMPRESSION_TYPE 0x00

/* Max cipher lines */
#define MAXLINE 255

/* Translate recv data into hex for parsing */
char *hexstring(const unsigned char*, size_t);
/* Allows us to timeout a connection */
void sig_alarm(int);
void process_ssl_hello(char *serverhello, char *c1, char *c2, char *c3, int verbose);
void process_ssl_alert(char *sslalert,char *c1, char *c2, char *c3, int verbose);
void DieWithError(char *errorMessage);
void make_ssl_debug(unsigned char *buf, char *msg, int len);
unsigned int hstr_i(const char *cptr);
unsigned char *build_ssl2_hello_msg(); // todo, stops -Wall errors
unsigned char *build_hello_msg(); // todo, stops -Wall errors
int new_socket(struct sockaddr_in *, char *servIP, unsigned short sslPort);
void new_connect(struct sockaddr_in *, int sock);
void new_write(int sock, unsigned char *pkt_out, int struct_len);
void new_read(); // todo, stops -Wall errors

struct getBytes {
	int totalBytesRcvd;
	char *recv_data;
};

struct ssl2_client_hello
{
	uint8_t rl_content_type;
	uint8_t rl_len;
	uint8_t client_handshake;
	/* 
		todo: We can define client version 3.0  
		which will tell server to switch modes 
		if available.
	*/
	uint16_t client_version;
	uint16_t cipher_len;
	uint16_t sessionID;
	uint16_t challenge_len;
	/*
		todo: We use a uint32 and then perform bitwise
		shift later to get uint24, I'm not sure
		how portable this will be.
	*/
	uint32_t CipherSuite;
	char random[14];
};

struct tls1_ssl3_client_hello
{
    uint8_t rl_content_type;  	/* Client Hello Handshake 22 (0x16) */
	uint16_t rl_client_version;	/* SSL/TLS version (0x0301) */
	uint16_t rl_len;			/* Length for rest of struct */
	uint8_t client_handshake;	/* handshake client hello 0x01 */
	uint8_t client_len_pad;
	uint16_t client_len;		/* Length for rest of struct */
	uint16_t client_version;	/* SSL/TLS version (0x0301) */
	//uint32_t gmt_unix_time; 	/* The current time and date in UNIX 32-bit format */
	char random_bytes[32]; 		/* 28 bytes generated by random number generated */
	uint8_t sessionID;    		/* opaque SessionID<0..32>, safe to leave as 0 */
	uint16_t cipher_len;   		/* cipher len, 2 bytes per cipher */
	uint16_t CipherSuite;  		/* Cryptographic suite selector 2 bytes */
	uint8_t compression_len;	/* compression length */
	uint8_t compression_method;	/* 0-255 safe to leave null or 00 */
};

/* cipher is 44 bytes in */
struct tls1_ssl3_server_hello
{
	uint8_t content_type;  /* handshake 22 (0x16) and message len */
    uint16_t ssl_version;        /* SSL/TLS version (0x0301) */
	uint16_t hello_len;		/* Server Hello len with handshake */
	uint8_t server_hello;	/* Server hello start */
	uint32_t server_hello_len;	/* Server hello len */
	uint16_t ssl_version_hello;	/* SSL/TLS version (0x0301) */
	uint32_t gmt_unix_time; /* The current time and date in UNIX 32-bit format */
    char random_bytes[28];  /* 28 bytes generated by random number generated */
    uint8_t SessionID_len;      /* opaque SessionID<0..32>, safe to leave as 0 */
	unsigned char *SessionID;
    uint16_t CipherSuite;    /* Cryptographic suite selector 2 bytes */
    uint8_t compression_method;     /* 0-255 safe to leave null or 00 */
};


